﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhotoMgr.src.common;
using popLib;
using System.Collections;

namespace PhotoMgr.src.common
{
    public partial class SearchBox : UserControl
    {
        public enum SearchBoxType { TextBox, ComboBox, ListBox, CheckBox, DateTimeBox }

        public ExMultiSelectorLabel LabelTitle { get; set; }
        public SearchBoxType BoxType { get; set; }
        public List<Control> DataControls { get; set; }

        public Hashtable DataValue
        {
            get
            {

                Hashtable data = new Hashtable();

                switch (BoxType)
                {
                    case SearchBoxType.TextBox:
                        if (StringLib.isValid(DataControls[0].Text))
                            data.Add(LabelTitle.SelectedValue, DataControls[0].Text);
                        break;
                    case SearchBoxType.ComboBox:
                        object svalue = ((ComboBox)DataControls[0]).SelectedValue;
                        if( StringLib.isValid(svalue) && svalue.ToString() != "%" )
                            data.Add(LabelTitle.SelectedValue, svalue);
                        break;
                    case SearchBoxType.ListBox:
                        object lvalue = ((ListBox)DataControls[0]).SelectedValue;
                        if( StringLib.isValid(lvalue) && lvalue.ToString() != "%" )
                            data.Add(LabelTitle.SelectedValue, lvalue);
                        break;
                    case SearchBoxType.CheckBox:
                        if (((CheckBox)DataControls[0]).CheckState != CheckState.Indeterminate)
                            data.Add(DataControls[0].Tag.ToString(), ((CheckBox)DataControls[0]).Checked);
                        break;
                    case SearchBoxType.DateTimeBox:
                        if (((DateTimePicker)DataControls[0]).ShowCheckBox)
                        {
                            if (((DateTimePicker)DataControls[0]).Checked)
                                data.Add(LabelTitle.SelectedValue + "_From", ((DateTimePicker)DataControls[0]).Value);
                            if (((DateTimePicker)DataControls[1]).Checked)
                                data.Add(LabelTitle.SelectedValue + "_To", ((DateTimePicker)DataControls[1]).Value);
                        }
                        else
                        {
                            data.Add(LabelTitle.SelectedValue + "_From", ((DateTimePicker)DataControls[0]).Value);
                            data.Add(LabelTitle.SelectedValue + "_To", ((DateTimePicker)DataControls[1]).Value);
                        }

                        break;
                }

                return data;
            }
        }

        public SearchBox()
        {
            InitializeComponent();
            DataControls = new List<Control>();
            this.Margin = new Padding(0);
        }

        /// <summary>
        /// init label data
        /// </summary>
        /// <param name="titles">title must be name=value, name: label's text, value: key data for search</param>
        private void initLabel(string[] titles)
        {
            flp_container.Controls.Clear();

            LabelTitle = new ExMultiSelectorLabel();
            LabelTitle.DataSourceString = titles;
            LabelTitle.Width = 148;
            LabelTitle.Height = 23;
            flp_container.Controls.Add(LabelTitle);
        }

        public SearchBox initTextBox(string[] titles)
        {
            initLabel(titles);

            TextBox control = new TextBox();
            control.Width = 210;
            flp_container.Controls.Add(control);

            DataControls.Clear();
            DataControls.Add(control);
            BoxType = SearchBoxType.TextBox;

            return this;
        }
        public SearchBox initTextBox(string title)
        {
            return initTextBox(new string[] { title });
        }

        public delegate void FillComboBoxDelegate(ComboBox control, bool isShowAll);
        public SearchBox initComboBox(string[] titles, FillComboBoxDelegate function, bool isShowAll = true)
        {
            initLabel(titles);

            ComboBox control = new ComboBox();
            control.Width = 210;
            control.DropDownStyle = ComboBoxStyle.DropDownList;
            function(control, isShowAll); //call function delegate
            flp_container.Controls.Add(control);

            DataControls.Clear();
            DataControls.Add(control);
            BoxType = SearchBoxType.ComboBox;

            return this;
        }
        public SearchBox initComboBox(string title, FillComboBoxDelegate function, bool isShowAll = true)
        {
            return initComboBox(new string[] { title }, function, isShowAll);
        }

        public delegate void InitStatusComboBoxDelegate(ComboBox control, Global.StatusType status, bool showSelectAll);
        public SearchBox initComboBox(string[] titles, InitStatusComboBoxDelegate function, Global.StatusType status, bool isShowAll = true)
        {
            initLabel(titles);

            ComboBox control = new ComboBox();
            control.Width = 210;
            control.DropDownStyle = ComboBoxStyle.DropDownList;
            function(control, status, isShowAll); //call function delegate
            flp_container.Controls.Add(control);

            DataControls.Clear();
            DataControls.Add(control);
            BoxType = SearchBoxType.ComboBox;

            return this;
        }
        public SearchBox initComboBox(string title, InitStatusComboBoxDelegate function, Global.StatusType status, bool isShowAll = true)
        {            
            return initComboBox(new string[]{title}, function, status, isShowAll);
        }
        

        public delegate void FillListBoxDelegate(ListBox control, bool isShowAll);
        public SearchBox initListBox(string[] titles, FillListBoxDelegate function, bool isShowAll = true)
        {
            initLabel(titles);

            ListBox control = new ListBox();
            control.Width = 210;
            function(control, isShowAll); //call function delegate
            flp_container.Controls.Add(control);

            DataControls.Clear();
            DataControls.Add(control);
            BoxType = SearchBoxType.ListBox;

            return this;
        }
        public SearchBox initListBox(string title, FillListBoxDelegate function, bool isShowAll = true)
        {
            return initListBox(new string[] { title }, function, isShowAll);
        }

        /// <summary>
        /// init checkbox
        /// </summary>
        /// <param name="title">title must be name=value, name: checkbox's text, value: key data for search</param>
        public SearchBox initCheckBox(string title, bool showBlankLabel = false, bool allowThreeState = true, CheckState state = CheckState.Indeterminate)
        {
            if (showBlankLabel)
            {
                initLabel(new string[] { "" });
            }
            else
            {
                flp_container.Controls.Clear();
            }

            string[] items = title.SplitByAny("=:");

            CheckBox control = new CheckBox();
            control.Text = items[0];
            control.Tag = items.Length > 1 ? items[1] : items[0];
            control.AutoSize = true;

            control.ThreeState = allowThreeState;
            control.CheckState = !allowThreeState && state == CheckState.Indeterminate ? CheckState.Unchecked : state;

            flp_container.Controls.Add(control);

            DataControls.Clear();
            DataControls.Add(control);
            BoxType = SearchBoxType.CheckBox;

            return this;
        }

        public SearchBox initDateTimeBox(string[] titles, bool isShowCheckBox = true, bool isChecked = false, string format = "dd/MM/yyyy")
        {
            initLabel(titles);

            DateTimePicker control = new DateTimePicker();
            control.Width = 98;
            control.Format = DateTimePickerFormat.Custom;
            control.CustomFormat = format;
            control.ShowCheckBox = isShowCheckBox;
            control.Checked = isChecked;
            control.Margin = new Padding(3, 3, 1, 3);
            flp_container.Controls.Add(control);

            Label label1 = new Label();
            label1.Width = 11;
            label1.Text = "-";
            label1.TextAlign = ContentAlignment.MiddleCenter;
            label1.Margin = new Padding(0, 2, 0, 0);
            flp_container.Controls.Add(label1);

            DateTimePicker control1 = new DateTimePicker();
            control1.Width = 98;
            control1.Format = DateTimePickerFormat.Custom;
            control1.CustomFormat = format;
            control1.ShowCheckBox = isShowCheckBox;
            control1.Checked = isChecked;
            control1.Margin = new Padding(2, 3, 3, 3);
            flp_container.Controls.Add(control1);

            DataControls.Clear();
            DataControls.Add(control);
            DataControls.Add(control1);
            BoxType = SearchBoxType.DateTimeBox;

            return this;
        }
        public SearchBox initDateTimeBox(string title, bool isShowCheckBox = true, bool isChecked = false, string format = "dd/MM/yyyy")
        {
            return initDateTimeBox(new string[] { title }, isShowCheckBox, isChecked, format);
        }

    }
}
